<template>
	<view>
		<u-empty class="noData" :show="addressList.length == 0" text="还没添加过地址噢~"></u-empty>
		<view class="addressList">
			<view class="addressCard" v-for="(res, index) in addressList" :key="index">
				<view class="item" @click="goBackWithAddress(res)">
					<view class="top-left">
						<view class="name">{{ res.name }}</view>
						<view class="phone">{{ res.phone }}</view>
						<view class="tag">
							<text class="tags" v-if="res.tag">{{ res.tag }}</text>
							<text v-if="res.isDefault" :class="{red:res.isDefault}">默认</text>
						</view>
					</view>
					<view class="bottom">
						<view class="address">
							{{res.address}}
						</view>
						<view class="area">
							{{res.area}}
						</view>
					</view>
				</view>
				<view class="top-right" @click="updateAddress(res)">
					<u-icon name="edit-pen" :size="30" color="#999999"></u-icon>
				</view>
			</view>
		</view>
		<view class="addSite" @click="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" :size="20"></u-icon>
				<text class="text">添加新地址</text>
			</view>
		</view>
	</view>
</template>

<script>
import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'
export default {
	data() {
		return {
			// addressList:[]
		};
	},
	onLoad() {
		this.getLocalAddressList();
		console.log(this.addressList)
	},
	computed: {
		...mapState('m_user',{
			addressList: state => state.addressList
		}),
		// ...mapState('m_user',['addressList']),
	},
	methods: {
		...mapMutations('m_user',['getLocalAddressList']),
		//新建地址
		toAddSite() {
			this.$u.route({
				type: 'to',
				params: {},
				url: '/subpkg/addAddress/addAddress',
				animationType: 'slide-in-bottom'
			});
		},
		//携带信息去修改地址
		updateAddress(info){
			this.$u.route({
				type: 'to',
				params: {
					addressInfo: JSON.stringify(info)
				},
				url: '/subpkg/addAddress/addAddress',
				animationType: 'slide-in-bottom'
			});
		},
		//返回上一个页面并传递数据
		goBackWithAddress(addressInfo) {
			console.log(getCurrentPages())
			let lastRoute = getCurrentPages().slice(-2,-1)[0].route
			console.log(lastRoute)
			if(lastRoute != "pages/my/my"){
				uni.$emit('getAddressInfo', addressInfo)
				uni.navigateBack()
			}
		}
	},
};
</script>

<style lang="scss" scoped>
.noData {
	padding-top: 50%;
	text-align: center;
}
.addressList {
	margin: 0 5px;
	padding: 5px;
	border: 2px solid rgba(108, 92, 92, 0.09);
	background-color: #ffffff;
	overflow: hidden;
	border-radius: 10px;
}
.addressCard {
	display: flex;
	flex-wrap: nowrap;
	border-bottom:1px solid #eeeeee;
	padding: 0;
}
.top-right {
	display: flex;
	justify-content: center;
	align-items: center;
}
.item {
	padding: 20rpx 10rpx;
	width: 90%;
	.top-left {
		display: flex;
		font-size: 13px;
		color: #606266;
		

		.phone {
			margin-left: 20rpx;
		}

		.tag {
			display: flex;
			font-weight: normal;
			align-items: center;

			text {
				display: block;
				padding: 0 10rpx;
				height: 30rpx;
				line-height: 30rpx;
				color: #ffffff;
				font-size: 10px;
				border-radius: 6rpx;
				text-align: center;
				margin-left: 30rpx;
				background-color: rgb(49, 145, 253);
			}
			.red {
				background-color: red
			}
			.tags{
				padding: 0 16rpx;
			}
		}
	}

	.bottom {
		display: flex;
		font-size: 14px;
		color: #303133;
		flex-wrap: wrap;
		
		.address {
			width: 100%;
			display: block;
			font-weight: bold;
			margin: 5rpx 0;
		}
		.area {
			font-size: 12px;
			color: #909399;
		}
	}
}

.addSite {
	display: flex;
	justify-content: space-around;
	width: 600rpx;
	line-height: 80rpx;
	position: absolute;
	bottom: 30rpx;
	left: 80rpx;
	background-color: royalblue;
	border-radius: 60rpx;
	font-size: 25rpx;

	.add {
		display: flex;
		align-items: center;
		color: #ffffff;

		.text {
			margin-left: 5px;
		}
	}
}
</style>
